

import 'dart:convert';

import 'package:isar_community/isar.dart';

part 'cardio_fitness_model.g.dart';


@collection
class CardioFitnessModel {

  CardioFitnessModel({
    this.userId,
    this.unixTime,
    this.dateTime,
    this.createTime,
    this.age,
    this.value,
    this.averageValue,
    this.maxValue,
    this.minValue,
    this.level,
    this.levelIntro,
    this.values = const [],
    this.firmwareVersion,
    this.firmwareId,
    this.macAddress,
    this.isUploaded
  });

  Id? id;

  /// 用户ID
  @Index()
  String? userId;

  /// 日期
  @Index()
  String? dateTime;
  //当天最新的最大摄氧量创建时间
  String? createTime;
  int? age;
  //当天最新的最大摄氧量
  int? value;
  //当天平均值
  double? averageValue;
  //当天最大值
  int? maxValue;
  //当天最小值
  int? minValue;
  //摄氧量等级
  int? level;
  //摄氧量等级描述
  String? levelIntro;

  List<CardioFitnessValueModel> values;
  //最新的记录时间
  int? unixTime;
  /// 是否上传到服务器
  bool? isUploaded;
  //固件 id
  int? firmwareId;
  //固件版本
  String? firmwareVersion;

  @Index()
  String? macAddress;

  factory CardioFitnessModel.fromJson(Map<String, dynamic> json) {
    return CardioFitnessModel(
      userId: "",
      value: json["value"],
      level: json["level"],
      levelIntro: json["levelIntro"],
      dateTime: json["dateTime"],
      unixTime: json["unixTime"], createTime: json["createTime"],
    );
  }

  Map<String, dynamic> toJson() => {
    'userId': userId,
    'value': value,
    "level": level,
    "dateTime": dateTime,
    "createTime": createTime,
    "levelIntro": levelIntro,
    "unixTime": unixTime,
    "values" : jsonEncode(values)
  };
}

@embedded
class CardioFitnessValueModel {

  CardioFitnessValueModel({
    this.value,
    this.createTime,
    this.level,
    this.age,
    this.unixTime,
    this.firmwareId,
    this.firmwareVersion,
    this.macAddress
  });

  String? createTime;
  int? value;
  int? age;
  int? level;
  int? unixTime;
  //固件 id
  int? firmwareId;
  //固件版本
  String? firmwareVersion;

  String? macAddress;

  Map<String, dynamic> toJson() => {
    'value': value,
    'createTime': createTime,
    "level": level,
    "unixTime": unixTime,
  };
}